﻿define(['util', 'ajax'], function (util, ajax) {
    "use strict";

    var exports = {},
         ViewModel;

    ViewModel = function () {

        var self = this;

        self.name = ko.observable();
        self.companyName = ko.observable();
        self.phone = ko.observable();
        self.email = ko.observable();
        self.description = ko.observable();
        self.reachMessage = ko.observable();

        var $reachMessage = $('#reachMessage');

        self.clear = function () {
            self.name('');
            self.companyName('');
            self.phone('');
            self.email('');
            self.description('');
        };

        self.save = function () {

            var form = {
                Name: self.name(),
                CompanyName: self.companyName(),
                Phone: self.phone(),
                Email: self.email(),
                Description: self.description(),
            };

            ajax.post('AnonymousLead', form).done(function (response) {
                if (response.id != '') {
                    self.clear();
                    $reachMessage.append($('<span class="h4">' + 'Thanks for giving us your valuable time. our representative will contact you soon.' + '</span>'));
                    $reachMessage.show();
                    setInterval(function () {
                        $reachMessage.hide();
                    }, 5000);
                }
            });
        };

        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(success);
        } else {
            alert("Geo Location is not supported on your current browser!");
        }

        function success() {
            var myLatlng = new google.maps.LatLng(23.85081, 72.11484);
            var mapOptions = {
                center: myLatlng,
                zoom: 12,
                mapTypeId: google.maps.MapTypeId.ROADMAP,
                scrollwheel: false,
                marker: true
            };
            var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
            var marker = new google.maps.Marker({
                position: myLatlng,
                map: map,
                title: "Your Location Address!"
            });
            marker.setMap(map);
            var infowindow = new google.maps.InfoWindow({ content: "<b>Sanelib Solutions</b><br/> Patan" });
            infowindow.open(map, marker);
        }
    };


    exports.exec = function (sectionId) {
        var viewModel = new ViewModel();
        ko.applyBindings(viewModel, document.getElementById(sectionId));
    };

    return exports;
});